package com.rules.admin.expression;


import com.gillion.grule.client.utils.BiFunction;
import com.gillion.grule.client.utils.Streams;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;

public class RulePackageExpression implements Expression, Serializable {
   private Package packageName;
   private List<RuleExpression> ruleExpressions = Lists.newArrayList();
   private List<Import> imports = Lists.newArrayList();
   private List<Attribute> attributes = Lists.newArrayList();

   public void setPackageName(Package packageName) {
      this.packageName = packageName;
   }

   public void attribute(Attribute attribute) {
      this.attributes.add(attribute);
   }

   public void addImport(Import importContent) {
      this.imports.add(importContent);
   }

   public void addRule(RuleExpression ruleExpression) {
      this.ruleExpressions.add(ruleExpression);
   }

   public RulePackageExpression(Package packageName) {
      this.packageName = packageName;
   }

   public void setRuleExpressions(List<RuleExpression> ruleExpressions) {
      this.ruleExpressions = ruleExpressions;
   }

   public List<RuleExpression> getRuleExpressions() {
      return this.ruleExpressions;
   }

   public Package getPackageName() {
      return this.packageName;
   }

   public void accept(ExpressionVisitor visitor) {
      visitor.vistor(this);
   }

   public String toString() {
      StringBuilder builder = new StringBuilder();
      builder.append(this.packageName).append("\n").append((String)Streams.of(this.imports).reduce(new BiFunction<String, Import>() {
         public String apply(String s, Import anImport) {
            String st = StringUtils.isEmpty(s) ? "" : s;
            return st + anImport.toString() + "\n";
         }
      })).append((String)Streams.of(this.attributes).reduce(new BiFunction<String, Attribute>() {
         public String apply(String s, Attribute attribute) {
            String st = StringUtils.isEmpty(s) ? "" : s;
            return st + attribute.toString() + "\n";
         }
      })).append((String)Streams.of(this.ruleExpressions).reduce(new BiFunction<String, RuleExpression>() {
         public String apply(String s, RuleExpression ruleExpression) {
            String st = StringUtils.isEmpty(s) ? "" : s;
            return st + ruleExpression.toString() + "\n";
         }
      }));
      return builder.toString();
   }
}
